package com.kpx.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;

import com.kpx.app.R;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;
import com.kpx.app.bean.AddressBean;
import com.kpx.app.activity.ActivityOrderAddAddress;
import com.kpx.app.activity.ActivityOrderChoiceAddress;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;

import java.util.List;

/**
 * User: 陈聪(617909447@qq.com)
 * Date: 2016-06-24
 * Time: 15:14
 * 选择配送地址适配器
 */
public class AddressAdapter extends ComAdapter<AddressBean> {

    /**
     * 当前选中位置
     */
    private int currentChecked;

    public AddressAdapter(Context context, List<AddressBean> datas, int layoutId) {
        super(context, datas, layoutId);
    }

    @Override
    public void convert(ComViewHolder holder, final AddressBean t, final int position) {
        holder.setText(R.id.txt_name_phone, t.getReceiver() + "  " + t.getPhone())
                .setText(R.id.txt_address, t.getRegion() + t.getAddress());

        //设置默认地址----------------------------------
        setDefaultChecked(holder, t, position);
        //提示地址是否超出配送范围----------------------------------
        setOutMention(holder, t);
        //设置点击事件----------------------------------
        setClick(holder, t);
    }

    /**
     * s设置相关点击事件
     *
     * @param holder
     * @param t
     */
    private void setClick(ComViewHolder holder, final AddressBean t) {
        holder.setOnClickListener(R.id.txt_to_edit, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(mContext.getResources().getString(R.string.intent_key_bean), t);
                ActivityOrderAddAddress.type = 1;
                IntentUtil.gotoActivityForResult(mContext, ActivityOrderAddAddress.class, bundle, ActivityOrderChoiceAddress.ADDRESS_CHANGE_REQUEST);
            }
        });
        holder.getConvertVeiw().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(mContext.getResources().getString(R.string.intent_key_bean), t);
                ((ActivityOrderChoiceAddress) mContext).setResult(((ActivityOrderChoiceAddress) mContext).RESULT_OK, intent);
                ((ActivityOrderChoiceAddress) mContext).finish();
            }
        });
    }

    /**
     * //提示地址是否超出配送范围
     *
     * @param holder
     * @param t
     */
    private void setOutMention(ComViewHolder holder, AddressBean t) {
        if (t.isOut() && t.getLatitude() != 0) {
            holder.setTextColor(R.id.txt_name_phone, R.color.grey_a1)
                    .setTextColor(R.id.txt_address, R.color.grey_a1)
                    .setTextColor(R.id.txt_to_edit, R.color.grey_a1);
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setTextColor(mContext.getResources().getColor(R.color.grey_a1));
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setEnabled(false);
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setButtonDrawable(mContext.getResources().getDrawable(R.drawable.super_market_cb_cart_grey_out));
            holder.setVisibility(R.id.img_is_out, ComViewHolder.VISIBLE);
        } else {
            holder.setVisibility(R.id.img_is_out, ComViewHolder.GONE);
            holder.setTextColor(R.id.txt_name_phone, R.color.black_43)
                    .setTextColor(R.id.txt_address, R.color.black_43)
                    .setTextColor(R.id.txt_to_edit, R.color.black_68);
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setTextColor(mContext.getResources().getColor(R.color.grey_a1));
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setEnabled(true);
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setButtonDrawable(mContext.getResources().getDrawable(R.drawable.super_market_sale_cb_selector));
        }
    }

    /**
     * 设置默认地址
     *
     * @param holder
     * @param t
     * @param position
     */
    private void setDefaultChecked(ComViewHolder holder, final AddressBean t, final int position) {
        if (t.getIsDefault() == 1) {
            currentChecked = position;
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setChecked(true);
        } else {
            ((CheckBox) holder.getView(R.id.rb_is_checked)).setChecked(false);
        }
        ((CheckBox) holder.getView(R.id.rb_is_checked)).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(final View v, MotionEvent event) {
                if (MotionEvent.ACTION_UP == event.getAction()) {
                    if (currentChecked != position) {
                        ((ActivityOrderChoiceAddress) mContext).showLoadingDialog();
                        saveOrUpdateAddress(t);
                    }
                }
                return true;
            }
        });
    }

    /**
     * 保存或修改地址
     * v1.0/app/user/subsidiary/addReceivingAddress
     * token,receiver(收货人),phone联系方式,region(广东省-深圳市),address(详细地址串),isDefault(默认地址1:是，2非)
     *
     */
    private void saveOrUpdateAddress(final AddressBean addressBean) {
        MyHashMap map = new MyHashMap();
        map.put("receiver", addressBean.getReceiver());
        map.put("phone", addressBean.getPhone() + "");
        map.put("region", addressBean.getRegion());
        map.put("address", addressBean.getAddress());
        map.put("isDefault", "1");
        String url = "";
        url = HttpDataReqUrl.URL_GET_ADDRESS_EDIT;
        map.put("receiverId", addressBean.getDmId() + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                ((ActivityOrderChoiceAddress) mContext).dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        mDatas.get(i).setIsDefault(2);
                    }
                    addressBean.setIsDefault(1);
                    notifyDataSetChanged();
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                } else {
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ((ActivityOrderChoiceAddress) mContext).dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(mContext, error);
            }

        }, AddressBean.class, HttpDataRequest.TYPE_OBJECT).POST(url, true, map);

    }
}
